package org.example.software.service.impl;

import org.example.software.entity.Forum;
import org.example.software.entity.SubForum;
import org.example.software.exception.MyException;
import org.example.software.mapper.ForumMapper;
import org.example.software.mapper.SubForumMapper;
import org.example.software.mapper.UserMapper;
import org.example.software.service.SubForumService;
import org.example.software.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class SubForumServiceImpl implements SubForumService {

    @Autowired
    private SubForumMapper subForumMapper;

    @Autowired
    private ForumMapper forumMapper;

    @Autowired
    private UserMapper userMapper;

    //嵌套子留言
    @Override
    public boolean addSubForum(String token, String content, boolean isForumParent, int parentId) {
        // 从 Token 中解析用户账号
        String userId = JwtUtil.getUserIdFromToken(token);
        if (userId == null || userId.isEmpty()) {
            MyException.throwError("无效的 Token 或没有登录账号", 401);
        }
        if (content == null || content.isEmpty()) {
            MyException.throwError("留言内容不能为空", 400);
        }

        // 设置用户信息
        SubForum subForum = new SubForum();
        subForum.setUserID(userId);
        subForum.setContent(content);
        subForum.setName(userMapper.getUserNameById(userId));
        subForum.setDate(LocalDateTime.now());

        // 验证父级 ID 的合法性
        if (isForumParent) {
            // 检查 Forum 表
            Forum parentForum = forumMapper.selectById(parentId);
            if (parentForum == null) {
                MyException.throwError("父评论不存在于 Forum 表中", 404);
            }
        } else {
            // 检查 SubForum 表
            SubForum parentSubForum = subForumMapper.selectById(parentId);
            if (parentSubForum == null) {
                MyException.throwError("父评论不存在于 SubForum 表中", 404);
            }
        }

        // 设置父级 ID
        subForum.setParentId(parentId);

        // 保存留言
        return subForumMapper.insert(subForum) > 0;
    }

    //删除留言
    @Override
    public boolean deleteSubForum(String token, int subForumId) {
        // 检查是否为管理员
        boolean isAdmin = JwtUtil.isAdmin(token);
        if (!isAdmin) {
            // 如果不是管理员，解析用户账号
            String userId = JwtUtil.getUserIdFromToken(token);
            if (userId == null || userId.isEmpty()) {
                MyException.throwError("无效的 Token 或没有登录账号", 401);
            }

            // 查询留言详情
            SubForum subForum = subForumMapper.selectById(subForumId);
            if (subForum == null) {
                MyException.throwError("留言不存在", 404);
            }

            // 检查是否是留言的创建者
            if (!userId.equals(subForum.getUserID())) {
                MyException.throwError("无权限删除该留言", 403);
            }
        } else {
            // 管理员无需校验用户身份，直接允许删除
            if (subForumMapper.selectById(subForumId) == null) {
                MyException.throwError("留言不存在", 404);
            }
        }

        // 执行删除操作
        return subForumMapper.deleteById(subForumId) > 0;
    }

}
